//
//  RecommendViewController.swift
//  SwiftDemo
//
//  Created by liguoqing on 2022/6/26.
//  Copyright © 2022 guoqing.li. All rights reserved.
//

import UIKit

private let kCycleViewH : CGFloat = kScreenW * 3 / 8;
private let kGameViewH : CGFloat = 90

class RecommendViewController : BaseAnchorViewController {
    
    private lazy var recomondViewModel: RecommendViewModel = RecommendViewModel()
    
    private lazy var cycleView: RecommendCycleView = {
        let cycleView = RecommendCycleView(frame: .zero)
        cycleView.backgroundColor = .orange
        return cycleView
    }()
    
    private lazy var gameView: RecommendGameView = {
        let gameView = RecommendGameView(frame: .zero)
        return gameView
    }()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        collectionView.backgroundColor = .white
        collectionView.register(RecommendPrettyViewCell.self, forCellWithReuseIdentifier: kPrettyCellID)
    }
    
}

extension RecommendViewController
{
    override func setupUI(){
        super.setupUI()

        collectionView.addSubview(cycleView)
        collectionView.addSubview(gameView)
        cycleView.snp.makeConstraints { make in
            make.leading.equalTo(0)
            make.top.equalTo(-kCycleViewH-kGameViewH)
            make.width.equalTo(kScreenW)
            make.height.equalTo(kCycleViewH)
        }
        gameView.snp.makeConstraints { make in
            make.top.equalTo(-kGameViewH)
            make.leading.equalTo(0)
            make.width.equalTo(kScreenW)
            make.height.equalTo(kGameViewH)
        }
        collectionView.contentInset = UIEdgeInsets(top: kCycleViewH + kGameViewH, left: 0, bottom: 0, right: 0)
    }
}

extension RecommendViewController
{
    override func loadData() {
        baseVM = recomondViewModel
        
        recomondViewModel.requestData {
            // 展示数据
            self.collectionView.reloadData()
            
            var groups = self.recomondViewModel.anchorGroups
            groups.removeFirst()
            groups.removeFirst()
                        
            // 添加更多
            let moreGroup = AnchorGroupModel()
            moreGroup.icon_name = "更多"
            
            groups.append(moreGroup)
            
            // 游戏模块
            self.gameView.groups = groups

            // 请求数据完成
            self.loadDataFinished()
            
        }
        
        recomondViewModel.requestCycleData {
            self.cycleView.cycleModels = self.recomondViewModel.cycleModels
        }
    }
}

extension RecommendViewController : UICollectionViewDelegateFlowLayout {
    override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        if(indexPath.section == 1){
            let prettyCell = collectionView.dequeueReusableCell(withReuseIdentifier: kPrettyCellID, for: indexPath) as! RecommendPrettyViewCell
            
            prettyCell.anchor = self.recomondViewModel.anchorGroups[indexPath.section].anchors[indexPath.item]
            
            return prettyCell
        }
       return super.collectionView(collectionView, cellForItemAt: indexPath)
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        if(indexPath.section == 1){
            return CGSize(width: kNormalItemW, height: kPrettyItemH)
        }
        return CGSize(width: kNormalItemW, height: kNormalItemH)
    }
}

